package cluster

import (
	replyInterface "go-redis/interface/resp"
	"go-redis/resp/reply"
)

// del命令的处理函数
func delFunc(cluster *ClusterDatabase, c replyInterface.Connection, cmdArgs [][]byte) replyInterface.Reply {
	replies := cluster.broadcast(c, cmdArgs)
	var errReply reply.ErrorReply
	var deleted int64 = 0
	for _, v := range replies {
		if reply.IsErrorReply(v) {
			errReply = v.(reply.ErrorReply)
			break
		}
		intReply, ok := v.(*reply.IntReply)
		if !ok {
			errReply = reply.MakeErrReply("error")
		}
		deleted += intReply.Code
	}
	if errReply != nil {
		return reply.MakeErrReply("error occurs: " + errReply.Error())
	}
	return reply.MakeIntReply(deleted)
}
